package com.xw.bo.ks.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xw.bo.ks.client.RedisClient;
import com.xw.bo.ks.entity.common.RedisCommon;
import com.xw.bo.ks.entity.enums.ExceptionEnum;
import com.xw.bo.ks.entity.exception.BaseException;
import com.xw.bo.ks.entity.exception.BoException;
import com.xw.bo.ks.service.UserRegisterInfoService;
import com.xw.bo.ks.mapper.UserRegisterInfoMapper;
import org.springframework.stereotype.Service;
import com.xw.bo.ks.entity.UserRegisterInfo;

import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.xw.bo.ks.entity.bean.UserRegisterInfoReq;
import com.xw.bo.ks.entity.bean.UserRegisterInfoQo;
import com.xw.bo.ks.util.XwUtil;
import org.apache.commons.lang3.StringUtils;

import com.xw.bo.ks.entity.base.BoPage;
import com.xw.bo.ks.entity.base.HeadDataPage;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @author： xw ftl auto
 * @create： 2024-08-22 16:21:46
 */
@Service
public class UserRegisterInfoServiceImpl extends ServiceImpl<UserRegisterInfoMapper, UserRegisterInfo> implements UserRegisterInfoService {

    @Resource
    private UserRegisterInfoMapper userRegisterInfoMapper;

    @Resource
    private RedisClient redisClient;

    @Override
    public BoPage<UserRegisterInfo> pageSampleUserRegisterInfo(Integer pageNumber, Integer pageSize) {

        HeadDataPage headDataPage = new HeadDataPage();
        headDataPage.setPageNumber(pageNumber);
        headDataPage.setPageSize(pageSize);

        return boPageBean(new QueryWrapper<>(), headDataPage);
    }

    @Override
    public BoPage<UserRegisterInfo> pageEqUserRegisterInfoDto(UserRegisterInfoReq userRegisterInfoReq) {

        UserRegisterInfo queryUserRegisterInfo = new UserRegisterInfo();
        BeanUtils.copyProperties(userRegisterInfoReq, queryUserRegisterInfo);

        LambdaQueryWrapper<UserRegisterInfo> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(null != userRegisterInfoReq.getUserRegisterInfoId(), UserRegisterInfo::getId, userRegisterInfoReq.getUserRegisterInfoId());
        queryWrapper.eq(null != userRegisterInfoReq.getUserId(), UserRegisterInfo::getUserId, userRegisterInfoReq.getUserId());
        queryWrapper.eq(null != userRegisterInfoReq.getName(), UserRegisterInfo::getName, userRegisterInfoReq.getName());
        queryWrapper.eq(null != userRegisterInfoReq.getPhone(), UserRegisterInfo::getPhone, userRegisterInfoReq.getPhone());
        queryWrapper.eq(null != userRegisterInfoReq.getPassword(), UserRegisterInfo::getPassword, userRegisterInfoReq.getPassword());
        queryWrapper.eq(null != userRegisterInfoReq.getUpdateTime(), UserRegisterInfo::getUpdateTime, userRegisterInfoReq.getUpdateTime());
        queryWrapper.eq(null != userRegisterInfoReq.getCreateTime(), UserRegisterInfo::getCreateTime, userRegisterInfoReq.getCreateTime());
        queryWrapper.eq(null != userRegisterInfoReq.getIsDelete(), UserRegisterInfo::getIsDelete, userRegisterInfoReq.getIsDelete());
        queryWrapper.eq(null != userRegisterInfoReq.getDeletedAt(), UserRegisterInfo::getDeletedAt, userRegisterInfoReq.getDeletedAt());
        queryWrapper.eq(null != userRegisterInfoReq.getCreatedAt(), UserRegisterInfo::getCreatedAt, userRegisterInfoReq.getCreatedAt());
        queryWrapper.eq(null != userRegisterInfoReq.getUpdatedAt(), UserRegisterInfo::getUpdatedAt, userRegisterInfoReq.getUpdatedAt());
        queryWrapper.eq(null != userRegisterInfoReq.getCreatedYmd(), UserRegisterInfo::getCreatedYmd, userRegisterInfoReq.getCreatedYmd());

        if (StringUtils.isNotEmpty(userRegisterInfoReq.getStartTime())) {
            Integer startTime = XwUtil.parseToInteger(userRegisterInfoReq.getStartTime());
            queryWrapper.ge(UserRegisterInfo::getCreatedAt, startTime);
        }
        if (StringUtils.isNotEmpty(userRegisterInfoReq.getEndTime())) {
            Integer endTime = XwUtil.parseToInteger(userRegisterInfoReq.getEndTime());
            queryWrapper.le(UserRegisterInfo::getCreatedAt, endTime);
        }

        queryWrapper.orderByDesc(UserRegisterInfo::getCreatedAt);

        return boPageBean(queryWrapper, userRegisterInfoReq);
    }


    @Override
    public BoPage<UserRegisterInfo> pageUserRegisterInfoMapper(UserRegisterInfoReq userRegisterInfoReq) {
        if (!"desc".equals(userRegisterInfoReq.getOrderValue()) && !"aes".equals(userRegisterInfoReq.getOrderValue())) {
            userRegisterInfoReq.setOrderValue("desc");
        }

        UserRegisterInfoQo userRegisterInfoQo = new UserRegisterInfoQo();
        BeanUtils.copyProperties(userRegisterInfoReq, userRegisterInfoQo);

        if (userRegisterInfoReq.getUserRegisterInfoId() != null) {
            userRegisterInfoQo.setId(userRegisterInfoReq.getUserRegisterInfoId());
        }

        if (StringUtils.isNotEmpty(userRegisterInfoReq.getStartTime())) {
            Integer startTime = XwUtil.parseToInteger(userRegisterInfoReq.getStartTime());
            userRegisterInfoQo.setStartTime(startTime);
        }
        if (StringUtils.isNotEmpty(userRegisterInfoReq.getEndTime())) {
            Integer endTime = XwUtil.parseToInteger(userRegisterInfoReq.getEndTime());
            userRegisterInfoQo.setEndTime(endTime);
        }

        return boPageBeanBack(new QueryWrapper<>(), userRegisterInfoReq, wrapper -> userRegisterInfoMapper.pageUserRegisterInfoMapper(userRegisterInfoQo));
    }

    @Override
    public String loginBackToken(String name, String password) {

        if (StringUtils.isEmpty(password)) {
            throw new BoException(ExceptionEnum.LOGIN_PASSWORD_EMPTY);
        }

        if (StringUtils.isEmpty(name)) {
            throw new BoException(ExceptionEnum.LOGIN_NAME_EMPTY);
        }

        String key = String.format(RedisCommon.USER_LOGIN_INFO_KEY, name);
        Object object = redisClient.get(key);
        if (object != null) {
            String tokenKey = String.format(RedisCommon.USER_LOGIN_TOKEN_KEY, object.toString());
            Object objectToken = redisClient.get(tokenKey);
            if (objectToken != null && objectToken instanceof UserRegisterInfo) {
                UserRegisterInfo registerInfo = (UserRegisterInfo) objectToken;
                if (registerInfo.getPassword().equals(password)) {
                    return object.toString();
                }
            }

        }

        LambdaQueryWrapper<UserRegisterInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRegisterInfo::getName, name);
        queryWrapper.eq(UserRegisterInfo::getPassword, password);

        UserRegisterInfo registerInfo = findOne(queryWrapper);
        if (registerInfo == null) {
            throw new BoException(ExceptionEnum.LOGIN_NAME_PASSWORD_ERROR);
        }


        String string = UUID.randomUUID().toString();
        String tokenKey = String.format(RedisCommon.USER_LOGIN_TOKEN_KEY, string);
        Object objectToken = redisClient.get(tokenKey);
        if (objectToken != null) {
            string = UUID.randomUUID().toString();
            tokenKey = String.format(RedisCommon.USER_LOGIN_TOKEN_KEY, string);
            objectToken = redisClient.get(tokenKey);
            if (objectToken != null) {
                throw new BoException(ExceptionEnum.LOGIN_TOKEN_ERROR);
            }
        }

        redisClient.set(key, string, 7200);
        redisClient.set(tokenKey, registerInfo, 86400);

        return string;
    }

    @Override
    public UserRegisterInfo getUserInfo(String token) {
        String tokenKey = String.format(RedisCommon.USER_LOGIN_TOKEN_KEY, token);
        Object objectToken = redisClient.get(tokenKey);
        if (null == objectToken) {
            return null;
        }

        return (UserRegisterInfo) objectToken;
    }


}
